package userConsole.service;

import org.json.JSONException;
import org.json.JSONObject;
import userConsole.jdbc.BaseJDBC;
import userConsole.jdbc.UserJDBC;
import userConsole.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Godv
 * @version 1.0
 */
public class LoginService {
    public JSONObject loginUser(String username, String password) throws JSONException {
        Connection connection = BaseJDBC.createConnection();
        UserJDBC userJDBC = new UserJDBC();
        JSONObject jsonObject = new JSONObject();
        try {
            User user = userJDBC.loginUser(connection, username);
            if(user != null) {
                if(user.getPassword().equals(password)) {
                    jsonObject.put("code", 200);
                    jsonObject.put("msg", "登录成功");
                    jsonObject.put("user", user);
                    return jsonObject;
                }
            }
            jsonObject.put("code", 306);
            jsonObject.put("msg", "用户名或密码错误");
        } catch (SQLException e) {
            jsonObject.put("code", 506);
            jsonObject.put("msg", "登录失败");
        }
        return jsonObject;
    }
}
